package com.gdones.gpt.bus.gptstaff.service.impl;

import com.gdones.common.core.dto.MyResult;
import com.gdones.gpt.bus.gptstaff.dto.StaffInfo;
import com.gdones.gpt.bus.gptstaff.mapper.StaffInfoMapper;
import com.gdones.gpt.bus.gptstaff.service.IStaffInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author GaoJingBo
 * @since 2023-06-04
 */
@Service
@RefreshScope
public class StaffInfoServiceImpl extends ServiceImpl<StaffInfoMapper, StaffInfo> implements IStaffInfoService {

    @Autowired
    private StaffInfoMapper infoMapper;

    @Value("${mygpt.newStaffTyDay}")
    private Integer newStaffTyDay;

    @Override
    public MyResult logForLogin(HttpServletRequest request,String staffID, String userName, boolean isLogin) {

        MyResult result = new MyResult();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowDate = sdf.format(new Date());
        StaffInfo info = new StaffInfo();
        info.setInfoTime(new Date());
        info.setStaffID(staffID);
        String msg = "用户："+userName+"，在"+nowDate+"时登录-"+(isLogin==true?"成功":"用户名或密码错误")+",IP:"+request.getHeader("X-Forwarded-For");
        info.setInfoDesc(msg);

        int insert = infoMapper.insert(info);
        if(insert>0){

        }else{
            result.setMsg(null);
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult logForActive(String staffID, String staffName, String activeCode,String gptVipTypeName,Integer activeDays) {

        MyResult result = new MyResult();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowDate = sdf.format(new Date());
        StaffInfo info = new StaffInfo();
        info.setInfoTime(new Date());
        info.setStaffID(staffID);

        String msg = "用户【"+staffName+"】进行【激活码激活】,激活类型：【"+gptVipTypeName+"】,激活时间："+nowDate+",激活天数：【"+activeDays+"】,激活码："+activeCode;

        info.setInfoDesc(msg);

        int insert = infoMapper.insert(info);
        if(insert>0){

        }else{
            result.setMsg(null);
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult logForRegister(String staffID, String staffName) {

        MyResult result = new MyResult();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowDate = sdf.format(new Date());
        StaffInfo info = new StaffInfo();
        info.setInfoTime(new Date());
        info.setStaffID(staffID);

        String msg = "账号于"+nowDate+"进行注册。";

        info.setInfoDesc(msg);

        int insert = infoMapper.insert(info);
        if(insert>0){

        }else{
            result.setMsg(null);
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult logForTiyanActive(String staffID, String staffName) {
        MyResult result = new MyResult();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowDate = sdf.format(new Date());
        StaffInfo info = new StaffInfo();
        info.setInfoTime(new Date());
        info.setStaffID(staffID);

        String msg = "账号于"+nowDate+"，进行【体验版】激活，体验时长【"+newStaffTyDay+"】天。";

        info.setInfoDesc(msg);

        int insert = infoMapper.insert(info);
        if(insert>0){

        }else{
            result.setMsg(null);
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult logForPayActive(String staffID, String userName, String name, String gptVipTypeName, Integer vipDayLmit, BigDecimal price) {

        MyResult result = new MyResult();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowDate = sdf.format(new Date());
        StaffInfo info = new StaffInfo();
        info.setInfoTime(new Date());
        info.setStaffID(staffID);

        String msg = "账号于"+nowDate+"，购买【"+name+"】-【"+price.doubleValue()+"】元，激活会员【"+gptVipTypeName+"】，会员时长【"+vipDayLmit+"】天。";

        info.setInfoDesc(msg);

        int insert = infoMapper.insert(info);
        if(insert>0){

        }else{
            result.setMsg(null);
            result.setCode("9999");
        }

        return result;
    }
}
